package com.webank.aigc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.webank.aigc.service.Alg.AIgcServiceImpl;
import com.webank.aigc.service.model.BigModelNew;
import com.webank.aigc.service.neo4j.Neo4jService;
import io.micrometer.common.util.StringUtils;
import jakarta.servlet.ServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Map;

@RestController
public class AIgcController {


    public static int code = 200;

    @Autowired
    private AIgcServiceImpl aIgcService;

    @Autowired
    private Neo4jService neo4jService;
    private Map<String, String> urlMap;

    @RequestMapping("/answerStream")
    public void getAnswerStream(@RequestBody String questionStr, ServletResponse httpResponse) throws InterruptedException, IOException {
        JSONObject info = JSON.parseObject(questionStr);
        JSONArray messages = info.getJSONArray("messages");
        JSONObject jsonObject = messages.getJSONObject(0);
        String question = String.valueOf(jsonObject.get("content"));
        Map<String, String> urlMap = aIgcService.getAnswer(question);

        Thread.sleep(1000);


        httpResponse.setContentType("text/html;charset=utf-8");


        PrintWriter  printWriter = httpResponse.getWriter();


        while (BigModelNew.threadPoolExecutor.getActiveCount() != 0){

//            code = 300;


            if (!BigModelNew.totalAnswer.equals("")) {
                try {

                    printWriter.print(BigModelNew.totalAnswer);

                    httpResponse.flushBuffer();

                    BigModelNew.totalAnswer = "";

                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }

        // 打印链接

        ArrayList<String> urls = new ArrayList<>();
        ArrayList<String> names = new ArrayList<>();
        urlMap.forEach((k,v)->{
            if(StringUtils.isBlank(k))return;
            urls.add(v);
            names.add(k);
        });



        printWriter.print("<br></br>");
        printWriter.print("参考资料：");
        httpResponse.flushBuffer();


        for (int i = 0; i < urls.size(); i++) {
            printWriter.println("<hr></hr>" + "<a  target=\"_blank\" download=\"\" href=\""+urls.get(i)+"\">"+names.get(i)+"</a>");
            httpResponse.flushBuffer();
        }

        httpResponse.flushBuffer();


        printWriter.print("All-End");
        httpResponse.flushBuffer();





        printWriter.close();

    }


    @RequestMapping("/answer")
    public void getAnswer(@RequestBody String questionStr, ServletResponse httpResponse) throws InterruptedException, IOException {
        JSONObject info = JSON.parseObject(questionStr);
        JSONArray messages = info.getJSONArray("messages");
        JSONObject jsonObject = messages.getJSONObject(0);
        String question = String.valueOf(jsonObject.get("content"));


        aIgcService.getAnswer(question);

        Thread.sleep(1000);


        httpResponse.setContentType("text/html;charset=utf-8");


        PrintWriter  printWriter = httpResponse.getWriter();


        while (BigModelNew.threadPoolExecutor.getActiveCount() != 0){};


        printWriter.print(BigModelNew.totalAnswer);

        httpResponse.flushBuffer();


        // 打印链接
        ArrayList<String> urls = new ArrayList<>();
        ArrayList<String> names = new ArrayList<>();

        printWriter.print("<br></br>"+"<br></br>");
        printWriter.print("<br></br>"+"<br></br>");
        printWriter.print("参考资料：");


        httpResponse.flushBuffer();

        urls.add("https://blog.csdn.net/mark_to_win/article/details/84564352");
        names.add("链接1");
        urls.add("https://blog.csdn.net/qq_43795348/article/details/106293227");
        names.add("链接2");
        urls.add("https://blog.csdn.net/toxicmmqm/article/details/52983739");
        names.add("链接3");


        for (int i = 0; i < urls.size(); i++) {
            printWriter.print("<a href=\""+urls.get(i)+"\">"+names.get(i)+"</a>");
            httpResponse.flushBuffer();
        }





        printWriter.close();

    }


}
